package com.l2j.test.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.l2j.test.model.entity.*;
import com.l2j.test.model.enums.ItemType;
import com.l2j.test.web.admin.mapper.*;
import com.l2j.test.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.l2j.test.web.admin.vo.attr.AttrValueVo;
import com.l2j.test.web.admin.vo.graph.GraphVo;
import com.l2j.test.web.admin.vo.room.RoomDetailVo;
import com.l2j.test.web.admin.vo.room.RoomItemVo;
import com.l2j.test.web.admin.vo.room.RoomQueryVo;
import com.l2j.test.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId() != null ;
        super.saveOrUpdate(roomSubmitVo);
        if (isUpdate){
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            queryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(queryWrapper);

            LambdaQueryWrapper<RoomAttrValue> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(queryWrapper1);

            LambdaQueryWrapper<RoomFacility> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(queryWrapper2);

            LambdaQueryWrapper<RoomLabel> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(queryWrapper3);

            LambdaQueryWrapper<RoomPaymentType> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(queryWrapper4);

            LambdaQueryWrapper<RoomLeaseTerm> queryWrapper5 = new LambdaQueryWrapper<>();
            queryWrapper5.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(queryWrapper5);
        }
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList){
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
        for (Long attrValueId : attrValueIds){
            RoomAttrValue roomAttrValue = new RoomAttrValue();
            roomAttrValue.setRoomId(roomSubmitVo.getId());
            roomAttrValue.setAttrValueId(attrValueId);
            roomAttrValues.add(roomAttrValue);
        }
        roomAttrValueService.saveBatch(roomAttrValues);

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds){
            RoomFacility roomFacility = new RoomFacility();
            roomFacility.setRoomId(roomSubmitVo.getId());
            roomFacility.setFacilityId(facilityInfoId);
            roomFacilities.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilities);

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        for (Long labelInfoId : labelInfoIds){
            RoomLabel roomLabel = new RoomLabel();
            roomLabel.setRoomId(roomSubmitVo.getId());
            roomLabel.setLabelId(labelInfoId);
            roomLabels.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabels);

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        for (Long paymentTypeId : paymentTypeIds){
            RoomPaymentType roomPaymentType = new RoomPaymentType();
            roomPaymentType.setRoomId(roomSubmitVo.getId());
            roomPaymentType.setPaymentTypeId(paymentTypeId);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypes);

        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        for (Long leaseTermId : leaseTermIds){
            RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
            roomLeaseTerm.setRoomId(roomSubmitVo.getId());
            roomLeaseTerm.setLeaseTermId(leaseTermId);
            roomLeaseTerms.add(roomLeaseTerm);
        }
        roomLeaseTermService.saveBatch(roomLeaseTerms);

    }

    @Override
    public IPage<RoomItemVo> getRoomList(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.getRoomList(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = super.getById(id);
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        List<AttrValueVo> attrValueVos = attrValueMapper.getAttrValueList(roomInfo.getId());
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectListByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        roomDetailVo.setGraphVoList(graphVoList);
        return roomDetailVo;
    }

    @Override
    public void removeByRoomId(Long id) {
        super.removeById(id);
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }
}




